package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import com.ccl.iot.privates.BitmapBuilder;

public class RgbLightControler extends DeviceControler {
	/*
	 * 	Static functions
	 */
	public static boolean RegisterControlerType() {
		return RegisterControlerType(RgbLightControler.class);
	}

	/*
	 * 	Non-static functions
	 */
	private IOTRgbLight iDevice;

	public RgbLightControler(IOTRgbLight aDevice) {
		super(aDevice);

		iDevice = aDevice;
	}

	@Override
	public void setDevice(IOTDevice aDevice) {
		super.setDevice(aDevice);

		iDevice = (IOTRgbLight) aDevice;
	}

	@Override
	protected boolean onTouchDown(float aX, float aY) {

		/*Rect rect = new Rect(DimmerIconBuilder.origin.x, DimmerIconBuilder.origin.y, (int)(iWidth*0.4), (int)(iWidth*0.4));
		if(rect.contains((int)aX, (int)aY)){
			if(mParent != null){
				ControlDimmerPop pop = new ControlDimmerPop(UIUtils.getContext(), iDevice, mParent);
				pop.setOnDismissListener(new OnDismissListener() {
					
					@Override
					public void onDismiss() {
						isShowPop = false;
					}
				});
				isShowPop = true;
			}
		}else{
			onBitmapChange();
		}*/

		onBitmapChange();
		return true;
	}

	@Override
	protected boolean onTouchMove(float aX, float aY) {
		return true;
	}

	@Override
	protected boolean onTouchUp(float aX, float aY) {
		if (iDevice != null && mView != null) {
			Rect rect = new Rect(mView.getLeft(), mView.getTop(), mView.getRight(), mView.getBottom());
			if (rect.contains((int) aX, (int) aY)) {
				iDevice.turn(iDevice.isOn() == false);
			}
		}
		onBitmapChange();
		return true;
	}

	protected boolean onTouchCancel(float aX, float aY) {

		if (iDevice != null && mView != null) {
			Rect rect = new Rect(mView.getLeft(), mView.getTop(), mView.getRight(), mView.getBottom());
			if (rect.contains((int) aX, (int) aY)) {
				iDevice.turn(iDevice.isOn() == false);
			}
		}

		onBitmapChange();

		return true;
	}

	private View mView;

	private Rect tPadding = new Rect(12, 12, 12, 12);

	public void setView(View view) {
		mView = view;
	}

	@Override
	public Bitmap getBitmap(int aWidth, int aHeight) {
		if (aWidth > 0 && aHeight > 0 && iDevice != null) {
			Bitmap tBackground = Bitmap.createBitmap(aWidth, aHeight, Config.RGB_565);
			Canvas tCanvas = new Canvas(tBackground);
			Bitmap tBitmap = null;
			// Rect tPadding = super.getPadding();
			int tWidth = Math.abs(aWidth - tPadding.left - tPadding.right);
			int tHeight = Math.abs(aHeight - tPadding.top - tPadding.bottom);
			if (iDevice.getImageCount() == 0) {
				tCanvas.drawColor(super.getBackColor());
				tBitmap = RGBLightIconProvider.Build(iDevice, DeviceIconProvider.KICON_STYLE_DEFAULT, tWidth, tHeight, super.getBackColor(), isTouchDown());
			} else {
				tBitmap = BitmapBuilder.GetBitmap(iDevice.getImage(), tWidth, tHeight, false);
				int getHighLigthColor = RGBLightIconProvider.GetHighLigthColor(iDevice, isTouchDown());
				tCanvas.drawColor(getHighLigthColor);
			}
			if (tBitmap != null) {
				tCanvas.drawBitmap(tBitmap, tPadding.top, tPadding.bottom, new Paint());
				// tBitmap.recycle();
			}

			tCanvas.save();
			tCanvas.restore();

			return tBackground;
		}
		return null;
	}

}
